<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" placeholder="请输入要搜索的内容" cancelButton="none" focus="true"></uni-search-bar>
		</view>
		<view class="sugg-list">
			<view class="sugg-item" v-for="(item,index) in searchResults" :key="index"
				@click="gotoDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-if="searchResults.length===0">
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,index) in histories" :key="index" @click="gotoGoodsList(item)">
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]') //	获取本地存储的历史搜索记录
		},
		data() {
			return {
				timer: null, //	定时器
				keyWord: '', //	关键字
				searchResults: [], //	搜索建议列表
				historyList: [], //	搜索历史
			};
		},
		methods: {
			async getSearchList() { //	获取搜索建议列表
				if (this.keyWord.trim() === '') {
					this.searchResults = []
					return
				}
				const res = await uni.$http.get(`/api/public/v1/goods/qsearch?query=${this.keyWord}`)
				console.log(res);
				if (res.statusCode !== 200) {
					return uni.$showMsg()
				}
				this.searchResults = res.data.message
			},
			input(e) { //	input处理事件
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.keyWord = e
					this.getSearchList()
				}, 500)
			},
			gotoDetail(goods_id) { //	跳转到详细页面
				this.saveSearchHistory()
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=${goods_id}`
				})
			},
			saveSearchHistory() { //	保存搜索记录
				const set = new Set(this.historyList)
				set.delete(this.keyWord)
				set.add(this.keyWord)
				this.historyList = Array.from(set)
				uni.setStorageSync('keyword', JSON.stringify(this.historyList))
			},
			clean() { //	清除搜索记录
				this.historyList = []
				uni.removeStorageSync('keyword')
			},
			gotoGoodsList(item) { //	通过搜索记录跳转到商品列表
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?query=${item}`
				})
			}
		},
		computed: {
			histories() { //	处理搜索记录列表
				return [...this.historyList].reverse()
			}
		},
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
				background-color: rgb(248, 248, 248);
				border-color: rgb(248, 248, 248);
				color: black;
				font-size: 14px;
			}
		}
	}
</style>
